﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace HindernisGame
{
    public class OptionMenu
    {
        private Game1 game;
        private SpriteBatch spriteBatch;
        private KeyboardState currentState;
        private Rectangle mainFrame;
        private menuButton soundB, onB, offB, zurueckB;
        private List<menuButton> mbs = new List<menuButton>();
        private int menuCount = 0;
        private int sperre = 20;

        public OptionMenu(Game1 game)
        {
            this.game = game;
            soundB = new menuButton(new Rectangle(100, 220, 294, 72), game.Content.Load<Texture2D>("menu/Sound"), 0, true);
            onB = new menuButton(new Rectangle(500, 220, 124, 72), game.Content.Load<Texture2D>("menu/On"), 1, false);
            offB = new menuButton(new Rectangle(500, 220, 145, 72), game.Content.Load<Texture2D>("menu/Off"), 2, false);
            zurueckB = new menuButton(new Rectangle(100, 520, 162, 36), game.Content.Load<Texture2D>("menu/Zurueck"), 2, false);

            mbs.Add(soundB);
            mbs.Add(onB);
            mbs.Add(offB);
            mbs.Add(zurueckB);

            LoadContent();
        }

        public void LoadContent()
        {
            spriteBatch = new SpriteBatch(game.GraphicsDevice);
            mainFrame = new Rectangle(0, 0, game.GraphicsDevice.Viewport.Width, game.GraphicsDevice.Viewport.Height);
        }

        public void Update(GameTime gameTime)
        {
            if (sperre > 0)
                sperre--;
            else
            {
                currentState = Keyboard.GetState();
                if (currentState.IsKeyDown(Keys.Down))
                {
                    menuCount++;
                    sperre = 10;
                    zurueckB.selected = !zurueckB.selected;
                    soundB.selected = !soundB.selected;
                }
                if (currentState.IsKeyDown(Keys.Up))
                {
                    menuCount--;
                    sperre = 10;
                    zurueckB.selected = !zurueckB.selected;
                    soundB.selected = !soundB.selected;
                }

                menuCount = Math.Abs(menuCount);                    

                if (currentState.IsKeyDown(Keys.Enter))
                {
                    sperre = 15;
                    switch (menuCount % 2)
                    {
                        case (0): //sound  
                            game.sound = !game.sound;
                            game.playSound();
                            break;
                        case (1)://zurueck
                            menuCount = 0;
                            zurueckB.selected = false;
                            soundB.selected = true;
                            game.setCurrentScreen("startScreen");
                            break;
                    }
                }
            }

        }


        public void Draw(GameTime gameTime)
        {

            spriteBatch.Begin();
            spriteBatch.Draw(game.background, mainFrame, Color.White);
            spriteBatch.Draw(game.logo, new Rectangle(100, -50, 824, 291), new Color(255, 255, 255, 255));
            soundB.Draw(spriteBatch, game.font);
            zurueckB.Draw(spriteBatch, game.font);
            if (game.sound)
                onB.Draw(spriteBatch, game.font);
            else
                offB.Draw(spriteBatch, game.font);

            spriteBatch.End();
        }
    }
}
